import com.javacommunity.model.dao.*;
import com.javacommunity.model.dao.impl.*;
;
import com.javacommunity.model.entity.*;
import com.javacommunity.util.Support;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author Cong Pham Dinh
 */


public class Test {

    UserDAO userDao = UserDAOImpl.getInstance();
    QuestionDAO questionDAO = QuestionDAOImpl.getInstance();
    PostDAO postDAO = PostDAOImpl.getInstance();
    TagDAO tagDAO = TagDAOImpl.getInstance();
    PostVotationDAO postVotationDAO = PostVotationDAOImpl.getInstance();
    CategoryDAO categoryDAO = CategoryDAOImpl.getInstance();
    QuestionVotationDAO questionVotationDAO = QuestionVotationDAOImpl.getInstance();
    AnswerVotationDAO answerVotationDAO = AnswerVotationDAOImpl.getInstance();
    CategoryTypeDAO categoryTypeDAO = CategoryTypeDAOImpl.getInstance();

    User getUserById(int userId) {
        return userDao.getUserById(userId);
    }

    Question getQuestionById(int questionId) {
        return questionDAO.getQuestionById(questionId);
    }

    Post getPostById(int postId) {
        return postDAO.getPostById(postId);
    }

    List<Post> getPosts(int param) {
        return postDAO.getPosts(param);
    }

    Tag getTagById(int tagId) {
        return tagDAO.getTagById(tagId);
    }

    int countVotePost(Post post) {
        return postVotationDAO.countVotation(post);
    }

    CategoryType getCategoryTypeById(int id) {
        return categoryTypeDAO.getCategoryTypeById(id);
    }

    boolean isVote(User user, Post up) {
        return postVotationDAO.isVote(user, up);
    }

    public int getNumberOfVotes(User user) {
        int numberOfVotes = 0;
        for (Post post : user.getPosts()) {
            numberOfVotes += postVotationDAO.countVotation(post);
        }
        for (Question question : user.getQuestions()) {
            numberOfVotes += questionVotationDAO.countVotation(question);
        }
        for (Answer answer : user.getAnswers()) {
            numberOfVotes += answerVotationDAO.countVotation(answer);
        }
        return numberOfVotes;
    }

    Category getCategoryById(int id) {
        return categoryDAO.getCategoryById(id);
    }

    boolean createPost(Post p) {
        return postDAO.createPost(p);
    }

    boolean updatePost(Post p) {
        return postDAO.updatePost(p);
    }

    boolean createVote(UserPost up) {
        return postVotationDAO.createVote(up);
    }

    boolean updateVote(UserPost up) {
        return postVotationDAO.updateVote(up);
    }

    int getVotes(Post p) {
        return postVotationDAO.countVotation(p);
    }

    UserPost getUserPost(User user, Post post) {
        return postVotationDAO.getUserPostByUP(user, post);
    }

    public static void main(String[] args) {
        Test t = new Test();
        User user = t.getUserById(1);
        Post post = t.getPostById(28);
        Category category = t.getCategoryById(1);
        Question question = t.getQuestionById(4);
        Tag tag = t.getTagById(1);
        CategoryType categoryType = t.getCategoryTypeById(1);
        if(t.isVote(user, post)){
            UserPost up = t.getUserPost(user, post);
            up.setVote(false);
            System.out.println(t.updateVote(up));
        }else{
            UserPost up = new UserPost(user, post, true);
            System.out.println(t.createVote(up));
        }
        System.out.println(question.getTags().size());
        System.out.println(tag.getQuestions().size());
//        System.out.println("Comments size before: "+post.getComments().size());
//        Comment comment = new Comment(user, post, new Date(), new Date(), "comment 2");
//        post.getComments().add(comment);
//        System.out.println(t.updatePost(post));
//        System.out.println("Comments size after: "+post.getComments().size());
    }
}

class PostVotationComparator implements Comparator<Post> {

    PostVotationDAO postVotationDAO = PostVotationDAOImpl.getInstance();

    @Override
    public int compare(Post post1, Post post2) {
        return postVotationDAO.countVotation(post2) - postVotationDAO.countVotation(post1);
    }

}
